package com.pansoft.kline.util;

import com.efounder.frame.language.MultiLanguageUtil;
import com.efounder.utils.ResStringUtil;
import com.pansoft.kline.helper.BtcKanConfigAndRateHelper;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.util.TCMathUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class MarketNumberUtils {

    private static final Double MILLION = 10000.0;
    private static final Double MILLION1 = 1000000.0;

    private static final Double BILLION = 100000000.0;
    private static final String MILLION_UNIT = ResStringUtil.getString(R.string.common_text_ten_thousand);
    private static final String BILLION_UNIT = ResStringUtil.getString(R.string.common_text_billion);

    /**
     * 将数字转换成以万为单位或者以亿为单位，因为在前端数字太大显示有问题
     *
     * @param amount 报销金额
     * @return
     * @author
     * @version 1.00.00
     * @date 2018年1月18日
     */
    public static String amountConversion(double amount) {
        boolean isMinus = false;
        if (amount < 0) {
            isMinus = true;
            amount = Math.abs(amount);
        }
        //最终返回的结果值
        String result = String.valueOf(amount);
        //四舍五入后的值
        double value = 0;
        //转换后的值
        double tempValue = 0;
        //余数
        double remainder = 0;

        //首先double 保留两位小数

        BigDecimal b = new BigDecimal(amount + "");
        amount = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        //金额大于1万 小于1亿
        if (amount > 10000 && amount < BILLION) {
            tempValue = amount / MILLION;
            remainder = amount % 100;

            //余数小于50则不进行四舍五入
//            if (remainder < 50) {
//                value = formatNumber(tempValue, 2, false);
//            } else {
            value = formatNumber(tempValue, 2, true);
            // }
            //如果值刚好是10000万，则要变成1亿
            if (value == MILLION) {
                result = zeroFill(value / MILLION) + ResStringUtil.getString(R.string.common_text_billion);
            } else {
                //中英文切换  英文中以100万为单位M结尾
                if(amount >= MILLION1){
                    value = formatNumber(tempValue/100, 2, true);
                    result = zeroFill(value) + ResStringUtil.getString(R.string.common_text_ten_thousand);
                }else {

                    if(MultiLanguageUtil.getInstance().getSetLanguageLocale().getCountry().equals("CN")){
                        result = zeroFill(value) + ResStringUtil.getString(R.string.common_text_ten_thousand);
                    }else {
                        value = formatNumber(tempValue*10, 2, true);
                        result = zeroFill(value) +"K";
                    }
                }
            }
        }
        //金额大于1亿
        else if (amount > BILLION) {
            tempValue = amount / BILLION;
            remainder = amount % BILLION;

            //余数小于50000000则不进行四舍五入
//            if (remainder < (BILLION / 2)) {
//                value = formatNumber(tempValue, 2, false);
//            } else {
            value = formatNumber(tempValue, 2, true);
            //  }
            //中文以亿结尾   英文以B结尾 B = 10亿
            if(MultiLanguageUtil.getInstance().getSetLanguageLocale().getCountry().equals("CN")){
                result = zeroFill(value) + ResStringUtil.getString(R.string.common_text_billion);
            }else {
                if(amount > BILLION*10){
                    value = formatNumber(tempValue/10, 2, true);
                    result = zeroFill(value) + ResStringUtil.getString(R.string.common_text_billion);
                }else {
                    value = formatNumber(tempValue*100, 2, true);
                    result = zeroFill(value) + ResStringUtil.getString(R.string.common_text_ten_thousand);
                }
            }

        } else {
            result = zeroFill(amount);
        }
//        System.out.println(result);
        if (isMinus) {
            return "-" + result;
        }
        return result;
    }


    /**
     * 对数字进行四舍五入，保留2位小数
     *
     * @param number   要四舍五入的数字
     * @param decimal  保留的小数点数
     * @param rounding 是否四舍五入
     * @return
     * @author
     * @version 1.00.00
     * @date 2018年1月18日
     */
    public static Double formatNumber(double number, int decimal, boolean rounding) {
        BigDecimal bigDecimal = new BigDecimal(number);

        if (rounding) {
            return bigDecimal.setScale(decimal, RoundingMode.HALF_UP).doubleValue();
        } else {
            return bigDecimal.setScale(decimal, RoundingMode.DOWN).doubleValue();
        }
    }

    /**
     * 对四舍五入的数据进行补0显示，即显示.00
     *
     * @return
     * @author
     * @version 1.00.00
     * @date 2018年1月23日
     */
    public static String zeroFill(double number) {
        String value = String.valueOf(number);

        if (value.indexOf(".") < 0) {
            value = value + ".00";
        } else {
            String decimalValue = value.substring(value.indexOf(".") + 1);

            if (decimalValue.length() < 2) {
                value = value + "0";
            }
        }
        return value;
    }


    /**
     * 计算净流入，单位亿
     */
    public static float calculateIncomeing(double income, double output) {

        BigDecimal incomeDecimal = new BigDecimal(income + "");
        BigDecimal outputDecimal = new BigDecimal(output + "");

        BigDecimal amount = incomeDecimal.subtract(outputDecimal);
        return amount.divide(new BigDecimal(Math.pow(10, 8) + ""), 2, BigDecimal.ROUND_HALF_UP).floatValue();
    }


    /**
     * 根据数量获取不同货币对应的总价值
     *
     * @param site         交易所id
     * @param currency     货币 CNY USDT等
     * @param wantCurrency 期待返回的货币类型价格 CNY USDT
     * @param count        数量
     * @return
     */
    public static double getRealPrice(String site, String currency, String wantCurrency, double count) {
        double price = 0d;
        try {
            String rate = BtcKanConfigAndRateHelper.getRate(null).getJSONObject(site).getString(currency);
            String wantRate = BtcKanConfigAndRateHelper.getRate(null).getJSONObject(site).getString(wantCurrency);
            //count数量的代币相当于美元的价值
            double USDValue = TCMathUtil.div(count + "", rate, 8);

            double wantCurrencyValue = TCMathUtil.mul(USDValue + "", wantRate);
            return wantCurrencyValue;
        } catch (Exception e) {
            e.printStackTrace();
            return price;
        }

    }

    /**
     * 测试方法入口
     *
     * @param args
     * @author
     * @version 1.00.00
     * @date 2018年1月18日
     */
    public static void main(String[] args) throws Exception {
//        amountConversion(120);
//        amountConversion(18166.35);
//        amountConversion(1222188.35);
//        amountConversion(129887783.5);
        amountConversion(242586531.2712692);

        amountConversion(18166.35);

    }


}